#!/bin/sh

. ../../func

URL=http://triptico.com/download
SRC=mp
PKG=mp_static
VER=3.3.18b       #old, but my favorite.
COMP=tar.gz
DESC="mp text editor"
ncurses=
CAT=BuildingBlock
CWD=`pwd`

ncurses_ver=6.0 #2015-08-08

build_ncurses() {
	extract ncurses-${ncurses_ver}.tar.gz
	cd ncurses-${ncurses_ver}
	opts="--prefix=${ncurses_dir}
--without-manpages
--without-progs
--without-tests
--disable-db-install
--without-ada
--without-gpm
--without-shared
--without-debug
--without-develop
--without-cxx
--without-cxx-binding
--disable-big-core
--disable-big-strings"
	_configure
	_make ${MKFLG} LDFLAGS=-static CFLAGS=\"$(_cflags)\"
	_make install
	cd ..
}

build() {
	ncurses_dir=${PWD}/ncurses
	build_ncurses
	export CFLAGS=$(${ncurses_dir}/bin/ncurses6-config --cflags)
	export LIBS=$(${ncurses_dir}/bin/ncurses6-config --libs)
	export NCURSESW_CFLAGS=$CFLAGS
	export NCURSESW_LIBS=$LIBS
	export NCURSES_CFLAGS=$LIBS
	export NCURSES_LIBS=$LIBS
	cd ${SRC}-${VER}
	# /etc/nanoroc - nanorc
	# /usr/share/nano/*.nanorc
	opts="--prefix=/usr --sysconfdir=/etc --without-pcre --without-gtk"
	_config_sh
	echo "-static $LIBS" > config.ldflags
	_make
	abort_if_file_not_found $SRC
	install -d -m 0755 $CWD/${PKG}-${VER}-${ARCH}/bin
	_install $SRC $CWD/${PKG}-${VER}-${ARCH}/bin
	cd -
	rm -rf ncurses*
}

package() {
	# add this recipe
	install -d -m 0755 ./${PKG}-${VER}-${ARCH}/build
	cat ${PKG}.petbuild > ./${PKG}-${VER}-${ARCH}/build/$PKG-build-recipe
	# move, don't package
	cp -a --remove-destination ./$PKG-$VER-$ARCH/* ../../00_${ARCH}
	rm -r ${SRC}-${VER}
	rm -r $PKG-$VER-$ARCH
	echo "moved to initrd"
	echo "done!"
}

# main
retrieve https://ftp.gnu.org/pub/gnu/ncurses/ncurses-${ncurses_ver}.tar.gz
retrieve ${SRC}-${VER}.${COMP}
[ "$DLD_ONLY" = "yes" ] && exit
rm -rf ${SRC}-${VER}
extract ${SRC}-${VER}.${COMP}
build
package
